package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goods;
import com.huidao.entity.Goodstype;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;

public class GoodsDaoImpl implements GoodsDao {
	
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs =null;
	
	@Override
	public boolean addGoods(Goods goods) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO GOODS(GOODS_NAME,GOODS_PRICE,GOODS_ACCOUNT,GOODSTYPE_ID)VALUES(?,?,?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, goods.getGoodsName());
			ps.setDouble(2,goods.getGoodsPrice());
			ps.setLong(3,goods.getGoodsAccount());
			ps.setLong(4,goods.getGoodstype().getGoodstypeId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean delGoods(Long id) {
		// TODO Auto-generated method stub
String sql = "DELETE FROM GOODS WHERE GOODS_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateGoods(Goods goods) {
		// TODO Auto-generated method stub
		String sql = "UPDATE GOODS SET GOODS_NAME=?,GOODS_PRICE=?,GOODS_ACCOUNT=?,GOODSTYPE_ID=?,USER_ID=?,GOODS_SHELVES=? " +
				"WHERE GOODS_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1,goods.getGoodsName());
			ps.setDouble(2, goods.getGoodsPrice());
			ps.setLong(3, goods.getGoodsAccount());
			ps.setLong(4, goods.getGoodstype().getGoodstypeId());
			ps.setLong(5, goods.getUser().getUserId());
			ps.setLong(6, goods.getGoodsShelves());
			ps.setLong(7, goods.getGoodsId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public List<Goods> getListPage(int currentPage, int pageSize, String namekey) {
		// TODO Auto-generated method stub
		List<Goods> list = new ArrayList<Goods>();
		String sql = "SELECT * FROM GOODS G "
				+ "LEFT JOIN GOODSTYPE S ON G.GOODSTYPE_ID=S.GOODSTYPE_ID "
				+ "LEFT JOIN USER U ON G.USER_ID=U.USER_ID WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND GOODS_NAME LIKE '%"+namekey+"%' ";
		}
			sql = sql +	" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				goods.setGoodsPrice(rs.getDouble("GOODS_PRICE"));
				goods.setGoodsAccount(rs.getLong("GOODS_ACCOUNT"));
				Goodstype gs = new Goodstype();
				gs.setGoodstypeId(rs.getLong("GOODSTYPE_ID"));
				goods.setGoodstype(gs);
				User user = new User();
				user.setUserId(rs.getLong("USER_ID"));
				user.setUserShopname(rs.getString("user_shopname"));
				user.setUserName(rs.getString("user_name"));
				user.setUserPhone(rs.getLong("user_phone"));
				goods.setUser(user);
				list.add(goods);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount(String namekey) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM GOODS WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND GOODS_NAME LIKE '%"+namekey+"%' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public List<Goods> getAllList() {
		// TODO Auto-generated method stub
		List<Goods> list = new ArrayList<Goods>();
		String sql = "SELECT * FROM GOODS G "
				+ "LEFT JOIN GOODSTYPE S ON G.GOODSTYPE_ID=S.GOODSTYPE_ID"
				+ "LEFT JOIN USER U ON G.USER_ID=U.USER_ID";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			while(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				goods.setGoodsPrice(rs.getDouble("GOODS_PRICE"));
				goods.setGoodsAccount(rs.getLong("GOODS_ACCOUNT"));
				Goodstype gs = new Goodstype();
				gs.setGoodstypeId(rs.getLong("GOODSTYPE_ID"));
				goods.setGoodstype(gs);
				User user = new User();
				user.setUserId(rs.getLong("USER_ID"));
				user.setUserShopname(rs.getString("user_shopname"));
				goods.setUser(user);
				list.add(goods);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public List<Goods> getSellerListPage(int currentPage, int pageSize, String namekey,Long id) {
		// TODO Auto-generated method stub
		List<Goods> list = new ArrayList<Goods>();
		String sql = "SELECT * FROM GOODS G "
				+ "LEFT JOIN GOODSTYPE S ON G.GOODSTYPE_ID=S.GOODSTYPE_ID "
				+ "LEFT JOIN USER U ON G.USER_ID=U.USER_ID WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND GOODS_NAME LIKE '%"+namekey+"%' ";
		}
		if(id!=null&&id!=0){
			sql +=" AND s.user_ID="+id;
		}
			sql = sql +	" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				goods.setGoodsPrice(rs.getDouble("GOODS_PRICE"));
				goods.setGoodsAccount(rs.getLong("GOODS_ACCOUNT"));
				Goodstype gs = new Goodstype();
				gs.setGoodstypeId(rs.getLong("GOODSTYPE_ID"));
				goods.setGoodstype(gs);
				list.add(goods);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public boolean goodsShelves(Goods goods) {
		String sql = "UPDATE GOODS SET GOODS_SHELVES=? " +
				"WHERE GOODS_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, goods.getGoodsShelves());
			ps.setLong(2, goods.getGoodsId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateGoodsAccount(Goods goods) {
		String sql = "UPDATE GOODS SET goods_Account=? " +
				"WHERE GOODS_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, goods.getGoodsAccount());
			ps.setLong(2, goods.getGoodsId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public Long getGoodsAccount(Goods goods) {
		String sql = "select goods_Account from goods WHERE GOODS_ID=? ";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, goods.getGoodsId());
			rs=ps.executeQuery();
			if(rs.next()){
				return rs.getLong(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0l;
	}

}
